<?php
	set_include_path (PHPPATH);
	require_once 'MDB2.php';
	/**
	 * 
	 * The users controller
	 * @author Tri
	 *
	 */
	class user extends Application 
	{
		/**
		 * 
		 * Constructor
		 */
		function __construct(&$tmpl)
		{			
			$this->tmpl = &$tmpl;		
		}

		/**
		 * 
		 * Default action
		 */
		function index()
		{	
            $this->login();
		}
        
        function addFavProduct(){
        	$sessionUserId = $this->getLoggedUser();
        	$userId = $_REQUEST['uid'];
        	if ($sessionUserId != $userId){
        		echo "false";
        		return;
        	}
        	$productModel = $this->getModel('ProductModel');
        	$productId = $_REQUEST['pid'];
        	if (!$productModel->check_product($productId)){
        		echo "false";
        		return;
        	}
        	$userProductModel = $this->getModel('UserProductModel');
        	if($userProductModel->addUserProductRelation($userId, $productId, 1)){
        		echo "true";
        	}
        	else{
        		echo "false";
        	}
        		
        }
        
		function removeFavProduct(){
        	$sessionUserId = $this->getLoggedUser();
        	$userId = $_REQUEST['uid'];
        	if ($sessionUserId != $userId){
        		echo "false";
        		return;
        	}
        	$productId = $_REQUEST['pid'];
        	$userProductModel = $this->getModel('UserProductModel');
        	if($userProductModel->removeUserProduct($userId, $productId, 1)){
        		echo "true";
        	}
        	else{
        		echo "false";
        	}		
		}
	}
?>